package com.open.capacity.company.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.SysRecruitment;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.company.service.SysRecruitmentService;
import com.open.capacity.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@Api(tags = "RECRUITMENT API")
public class SysRecruitmentController {

    @Autowired
    private SysRecruitmentService sysRecruitmentService;

    /**
     * 公司查询
     * http://192.168.3.2:7000/users?access_token=3b45d059-601b-4c63-85f9-9d77128ee94d&start=0&length=10
     * @param params //  searchKey=username, searchValue=as
     * @return
     * @throws ControllerException
     */
//    @PreAuthorize("hasAuthority('company:get/name')")
    @ApiOperation(value = "用户查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/recruitment")
    @LogAnnotation(module = "company-center", recordRequestParam = false)
    public PageResult<SysRecruitment> findRecruitment(@RequestParam Map<String, Object> params) throws ControllerException {

        try {
            return sysRecruitmentService.findRecruitment(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }



//
//
    /**
     * 修改公司状态
     * @param params
     * @return
     * @throws ControllerException
     * @author gitgeek
     */
    @ApiOperation(value = "修改公司状态")
    @GetMapping("/recruitment/updateEnabled")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公司id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "enabled", value = "是否启用", required = true, dataType = "Boolean")
    })
    @LogAnnotation(module = "company-center", recordRequestParam = false)
//    @PreAuthorize("hasAnyAuthority('Recruitment:get/users/updateEnabled' ,'user:put/users/me')")
    public Result updateEnabled(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
            Long id = MapUtils.getLong(params, "id");
            if (id == 1277137734524300032L) {
                return Result.failed("超级管理员不给予修改");
            }
            return sysRecruitmentService.updateEnabled(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @PostMapping("/company/delre/{id}")
    @ApiOperation(value = "后台管理删除招聘信息")
    @LogAnnotation(module="company-center",recordRequestParam=false)
    public Result deletere(@PathVariable Long id) throws ControllerException {
        try {

            sysRecruitmentService.delete(id);
            return Result.succeed("操作成功");
        }catch (Exception e){
            throw new ControllerException(e);
        }
    }

    /**
     * 新增or更新
     * @param sysRecruitment
     * @return
     * @throws ControllerException
     */
    @PostMapping("/recruitment/save")
//    @PreAuthorize("hasAnyAuthority('user:post/users/saveOrUpdate')")
    @LogAnnotation(module = "company-center", recordRequestParam = false)
    public Result saveOrUpdate(@RequestBody SysRecruitment sysRecruitment)  {

        try {
            return sysRecruitmentService.saveOrUpdate(sysRecruitment);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }



}
